package cn.itcast.nems.journal.service.impl;

import cn.itcast.nems.journal.converter.JournalChangeDateConverter;
import cn.itcast.nems.journal.dto.JournalChangeDateLineCreate;
import cn.itcast.nems.journal.dto.JournalChangeDateLineDTO;
import cn.itcast.nems.journal.service.JournalChangeDateLineService;
import cn.itcast.nems.studentrecord.dao.entity.JournalChangeDateLine;
import cn.itcast.nems.studentrecord.dao.mapper.JournalChangeDateLineMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 改期凭单-行 服务实现类
 * </p>
 *
 * @author liyong
 * @since 2024-01-11
 */
@Service
@AllArgsConstructor
@Slf4j
public class JournalChangeDateLineServiceImpl extends ServiceImpl<JournalChangeDateLineMapper, JournalChangeDateLine> implements JournalChangeDateLineService {

    @Override
    public List<JournalChangeDateLineDTO> findJournalLines(String journalId) {
        return super.getBaseMapper()
                .selectLines(journalId)
                .stream()
                .map(r -> {
                    JournalChangeDateLineDTO dto = new JournalChangeDateLineDTO();
                    BeanUtils.copyProperties(r, dto);
                    return dto;
                })
                .toList();
    }

    @Override
    public Map<Integer, String> creatJournalChangeDateLines(List<JournalChangeDateLineCreate> createCmds) {
        final List<JournalChangeDateLine> lines = createCmds
                .stream()
                .map(JournalChangeDateConverter::convert)
                .toList();
        super.saveBatch(lines);
        Map<Integer, String> result = new HashMap<>(lines.size());
        lines.forEach(l -> result.put(l.getStudentId(), l.getId()));
        return result;
    }
}
